<?php

/**
 * Description of dnspod
 *
 * @author chengxuan
 */
class Dnspod {

    /**
     * 用户名
     *
     * @var string
     */
    protected $_username;

    /**
     * 密码
     *
     * @var string
     */
    protected $_password;

    /**
     * 语言
     *
     * @var string
     */
    protected $_lang;

    /**
     * API基本接口
     *
     * @var string
     */
    protected $_base_url = 'https://dnsapi.cn/%s';

    /**
     * 构造方法，初始化配置
     *
     * @param string $username 用户名
     * @param string $password 密码
     * @param string $lang     语言
     */
    public function __construct($username, $password, $lang='cn') {
        $this->_username = $username;
        $this->_password = $password;
        $this->_lang = $lang;
    }

    /**
     * POST提交数据
     *
     * @param string $uri   请求地址
     * @param array  $param 请求参数
     *
     * @return array
     */
    protected function _post($uri, array $param) {
        //组装默认参数
        $param['login_email'] = $this->_username;
        $param['login_password'] = $this->_password;
        $param['lang'] = $this->_lang;
        $param['error_on_empty'] = 'yes';
        $param['format'] = 'json';
        $user_agent = 'CHENGXUAN DDNS Client/1.0.0 (lcx165@gmail.com)';

        //CURL请求数据
        $url = sprintf($this->_base_url, $uri);
        $ch = curl_init();
        curl_setopt_array($ch, array(
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_URL             => $url,
            CURLOPT_USERAGENT       => $user_agent,
            CURLOPT_POSTFIELDS      => http_build_query($param),
        ));
        $result = curl_exec($ch);
        $result = json_decode($result, true);
        if($result === false) {
            $errno = curl_errno($ch);
            $error = curl_error($ch);
            print_r($error);exit;
        }

        return $result;
    }

    /**
     * 获取域名信息
     *
     * @param  string $domain
     *
     * @return array
     */
    public function domainInfo($domain) {
        return $this->_post('Domain.Info', array(
            'domain'    => $domain,
        ));
    }



    public function recordModify() {
        return $this->_post('Record.Modify', array(

        ));
    }

    public function recordDdns($domain_id, $record_id, $sub_domain, $record_line) {
        return $this->_post('Record.Ddns', array(
            'domain_id'   => $domain_id,
            'record_id'   => $record_id,
            'sub_domain'  => $sub_domain,
            'record_line' => $record_line,
        ));
    }

    public function monitorListsubvalue($domain, $subdomain) {
        return $this->_post('Monitor.Listsubvalue', array(
            'domain'    => $domain,
            'subdomain' => $subdomain,
        ));
    }




}
